//
//  TTEmotionTooBar.swift
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/29.
//  Copyright © 2016年 陶路路. All rights reserved.
//

import UIKit

let valeuNum:Int = 10086

enum TTEmotionTooBarType : Int{
    
    case Defalut = 0
    case Emoji = 1
    case Lxh = 2
}

class TTEmotionTooBar: UIStackView {
    
    var currentButton:UIButton?

    var closeBlock:((type:TTEmotionTooBarType)->())?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI(){
        axis = .Horizontal
        distribution = .FillEqually
        addButton("left", title: "默认",type:.Defalut )
        addButton("mid", title: "Emoji",type: .Emoji)
        addButton("right", title: "浪小花",type: .Lxh)
    }
    
    func addButton(img:String,title:String,type:TTEmotionTooBarType){
        let button = UIButton()
        if type == .Defalut{
            
            setButton(button)
        }
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(img)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(img)_selected"), forState: UIControlState.Selected)
        button.addTarget(self, action: "didClickButton:", forControlEvents: UIControlEvents.TouchUpInside)
//  防止tag 冲突 额外加上一个数字
        button.tag = type.rawValue + valeuNum
        addArrangedSubview(button)
    }
    
    func setCurrentSender(type:Int){
        
        let button = viewWithTag(type + valeuNum) as! UIButton
        
        setButton(button)
        
    }
    
    func setButton(sender:UIButton){
        
        if currentButton == sender{
            
            sender.selected = true
            
        }else{
            
            currentButton?.selected = false
            sender.selected = true
    }
        currentButton = sender
}
    
    func didClickButton(sender:UIButton){
        
        setButton(sender)
        
        closeBlock?(type:TTEmotionTooBarType(rawValue: sender.tag - valeuNum)!)
}

}